package com.taldh.algorithm.动态规划;

import org.junit.Assert;
import org.junit.Test;

public class Rob {

    public int rob(int[] nums) {
        if (nums == null || nums.length <= 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0],nums[1]);
        }
        int len = nums.length;
        int f0 = nums[0];
        int f1 = Math.max(nums[0], nums[1]);
        for (int i = 2; i < len; i++) {
            if (i % 2 == 0) {
                f0 = Math.max(f0 + nums[i], f1);
            } else {
                f1 = Math.max(f1 + nums[i], f0);
            }
        }
        return Math.max(f0, f1);
    }

    @Test
    public void test() {
        Assert.assertEquals(4, rob(new int[]{1,2,3,1}));
    }

    @Test
    public void test1() {
        Assert.assertEquals(4, rob(new int[]{2,1,1,2}));
    }
}